//
//  FundDataInfoViewController.m
//  Article
//
//  Created by Haricot_cpc17 on 16/5/9.
//  Copyright © 2016年 mac. All rights reserved.
//
//佣金详情
#import "FundDataInfoViewController.h"

@interface FundDataInfoViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableDictionary *mutDic;

@property (nonatomic,strong)NSMutableDictionary *mutDic2;

@end

@implementation FundDataInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"佣金详情";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    

    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont fontWithName:@"Heiti SC" size:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate =(id)self;
    
    _mutDic = [[NSMutableDictionary alloc]init];
    _mutDic2 = [[NSMutableDictionary alloc]init];
    [self loadThisPageData];
    [self createUI];
    
    // Do any additional setup after loading the view.
}
-(void)rightClick{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"领取佣金" message:@"确定领取佣金？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"领取", nil];
    alertView.delegate = self;
    [alertView show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        NSLog(@"取消");
    }if (buttonIndex == 1) {
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        
        NSMutableDictionary *mutDic = [[NSMutableDictionary alloc]init];
        
        [mutDic setObject:[user objectForKey:@"token"] forKey:@"user_token"];
        [mutDic setObject:_idStr forKey:@"brokerage_id"];
        
        [manager POST:@"http://wz.ijiaque.com/app/toup/usebrokerage.html" parameters:mutDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
            NSString * str = [NSString stringWithFormat:@"%@",[dic objectForKey:@"status"]];
            
            if ([str isEqualToString:@"1"]) {
                _mutDic2 = dic[@"data"];
                NSLog(@">>>%@",_mutDic2);
                NSLog(@"领取了佣金");
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                NSLog(@"该笔佣金已经领取过了");
            }
            [self.view makeToast:dic[@"info"] duration:0.5 position:CSToastPositionCenter];
        }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error.description);
            [self.view makeToast:@"网络连接失败，请检查网络连接" duration:0.5 position:CSToastPositionCenter];
        }];
    }
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)loadThisPageData{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    NSMutableDictionary *mutDic = [[NSMutableDictionary alloc]init];
    
    [mutDic setObject:[user objectForKey:@"token"] forKey:@"user_token"];
    [mutDic setObject:_idStr forKey:@"brokerage_id"];
    
    [manager POST:@"http://wz.ijiaque.com/app/toup/brokeragelog.html" parameters:mutDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSString * str = [NSString stringWithFormat:@"%@",[dic objectForKey:@"status"]];
        if ([str isEqualToString:@"1"]) {
            _mutDic = dic[@"data"];
        }
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [_tableView reloadData];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error.description);
        [self.view makeToast:@"网络连接失败，请检查网络连接" duration:0.5 position:CSToastPositionCenter];
    }];
}

-(void)createUI{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"fundDataInfoID"];
    _tableView.scrollEnabled = NO;
    
    UIView *btnView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    _tableView.tableFooterView = btnView;
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(30, 0, WIDTH - 60, 35)];
    
    if ([_statusStr isEqualToString:@"unused"]) {
        btn.backgroundColor = UIColorFromRGB(MYRed);
        [btn setTitle:@"立即领取" forState:UIControlStateNormal];
        btn.userInteractionEnabled = YES;
    }if ([_statusStr isEqualToString:@"used"]) {
        btn.backgroundColor = UIColorFromRGB(MYGray);
        [btn setTitle:@"该佣金已经领取过" forState:UIControlStateNormal];
        btn.userInteractionEnabled = NO;
    }
    btn.titleLabel.font = [UIFont fontWithName:@"Heiti SC" size:15];
    btn.layer.cornerRadius = 5;
    [btnView addSubview:btn];
    [btn addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"fundDataInfoID"];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"交易号:";
        cell.detailTextLabel.text = _mutDic[@"order_no"];
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"金额";
        cell.detailTextLabel.text = _mutDic[@"price"];;
    }
    if (indexPath.row == 2) {
        cell.textLabel.text = @"佣金入账方式";
        cell.detailTextLabel.text = _mutDic[@"category"];
    }
    if (indexPath.row == 3) {
        cell.textLabel.text = @"备注详情";
        cell.detailTextLabel.text = _mutDic[@"remark"];
        cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
        cell.detailTextLabel.numberOfLines = 0;
    }
    if (indexPath.row == 4) {
        cell.textLabel.text = @"交易时间";
        cell.detailTextLabel.text = _mutDic[@"create_time"];
    }
    cell.textLabel.font = [UIFont fontWithName:@"Heiti SC" size:13];
    cell.detailTextLabel.font = [UIFont fontWithName:@"Heiti SC" size:12];
    cell.userInteractionEnabled = NO;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 3) {
        return 80;
    }else{
        return 50;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
